﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiAbstractions.Community.ImageGallery;
using EPiServer.Common;
using EPiServer.Community.ImageGallery;
using EPiServer.Community.ImageGallery.Queries;
using JoelAbrahamsson.TestUtilities;
using Moq;
using StructureMap.AutoMocking;
using Xunit;

namespace JoelAbrahamsson.BlogExtensions.Tests
{
    public class AvatarProviderTests
    {
        [Fact]
        public void GravatarImageGalleryProperty_CreatesNewGalleryIfNoneExists()
        {
            EPiServerCommonSetup.InitCacheAndHttpContextWithAClearCache();
            MoqAutoMocker<AvatarProvider> avatarProviderAutoMocker = new MoqAutoMocker<AvatarProvider>();
            Mock<IImageGalleryHandlerFacade> fakeImageGalleryHandler = Mock.Get(avatarProviderAutoMocker.Get<IImageGalleryHandlerFacade>());
            ImageGallery savedGallery = new Mock<ImageGallery>().Object;
            fakeImageGalleryHandler.Setup(handler => handler.GetQueryResult(
                It.IsAny<ImageGalleryQuery>())).Returns(new ImageGalleryCollection());
            fakeImageGalleryHandler.Setup(handler => handler.AddImageGallery(
                It.Is<ImageGallery>(gallery => gallery.Name == AvatarProvider.GravatarImageGalleryName))).Returns(savedGallery);
            
            ImageGallery returnedGallery = avatarProviderAutoMocker.ClassUnderTest.GravatarImageGallery;

            Assert.Equal<ImageGallery>(savedGallery, returnedGallery);
        }

        [Fact]
        public void GravatarImageGalleryProperty_ReturnsExistingGallery()
        {
            EPiServerCommonSetup.InitCacheAndHttpContextWithAClearCache();
            MoqAutoMocker<AvatarProvider> avatarProviderAutoMocker = new MoqAutoMocker<AvatarProvider>();
            Mock<IImageGalleryHandlerFacade> fakeImageGalleryHandler = Mock.Get(avatarProviderAutoMocker.Get<IImageGalleryHandlerFacade>());
            ImageGallery existingGallery = new Mock<ImageGallery>().Object;
            ImageGalleryCollection imageGalleries = new ImageGalleryCollection { existingGallery };
            fakeImageGalleryHandler.Setup(handler => handler.GetQueryResult(
                It.IsAny<ImageGalleryQuery>())).Returns(imageGalleries);

            ImageGallery returnedGallery = avatarProviderAutoMocker.ClassUnderTest.GravatarImageGallery;

            Assert.Equal<ImageGallery>(existingGallery, returnedGallery);
        }

        //[Fact]
        //public void GetAvatarUrl_ReturnsUrlForExistingAvatar()
        //{
        //    EPiServerCommonSetup.InitCacheAndHttpContextWithAClearCache();
        //    MoqAutoMocker<AvatarProvider> avatarProviderAutoMocker = new MoqAutoMocker<AvatarProvider>();
        //    Mock<IImageGalleryHandlerFacade> fakeImageGalleryHandler = Mock.Get(avatarProviderAutoMocker.Get<IImageGalleryHandlerFacade>());
        //    Mock<IAuthor> fakeAuthor = new Mock<IAuthor>();
        //    fakeAuthor.Setup(author => author.Email).Returns(RandomValues.String);
        //    Mock<Image> fakeImage = new Mock<Image>();
        //    Mock<Thumbnail> fakeThumbnail = new Mock<Thumbnail>();
        //    string thumbnailUrl = RandomValues.String;
        //    fakeThumbnail.Setup(thumbnail => thumbnail.Url).Returns(thumbnailUrl);
        //    fakeImage.Setup(image => image.GetThumbnail(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<ThumbnailFormat>())).Returns(fakeThumbnail.Object);
        //    fakeImageGalleryHandler.Setup(handler => handler.GetQueryResult(It.IsAny<ImageQuery>())).Returns(new ImageCollection { fakeImage.Object });

        //    string returnedUrl = avatarProviderAutoMocker.ClassUnderTest.GetAvatarUrl(fakeAuthor.Object);

        //    Assert.Equal<string>(thumbnailUrl, returnedUrl);
        //}
    }
}
